<?php

namespace App\Http\Controllers\admin;

use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\common\PublicController;
//模型
use App\Models\Area;
use App\Models\Room;
use App\Models\Areamember;

class AreaController extends PublicController
{
    //区域列表页面
    public function getIndex(Request $request)
    {
        /*搜索条件始*/
        //搜索条件
        $get['area'] = $request -> input('area','');
        if($get['area']){
            $this -> where .= ' -> where("name","like","%".$get["area"]."%")';
        }
        /*搜索条件末*/
        /*代理商搜索始*/
        //获取自身及旗下所有代理商
        $agentInfo = $this -> agentInfo($request -> cookie('logon')['agent_id']);
        /*代理商搜索末*/
        $areaInfo = $this -> rangeInfo('area',$get);
        //获取区域表里代理商下的所有数据
        $data = $this -> memberInRange('area',$areaInfo -> all());
        $get['agent_id'] = $this -> agent_id;
        return view('admin.area.index',['data'=>$data,'areaInfo'=>$areaInfo,'agentInfo'=>$agentInfo,'request'=>$request->all(),'get'=>$get,'areaids' => $this->area_ids]);
    }

    //区域添加页面
    public function getCreate(Request $request)
    {
        return view('admin.area.create');
    }

    //添加处理页面
    public function postStore(Request $request)
    {
        $info = $request -> except('_token');
        $info['agent_id'] = $this -> agent_id;
        //添加区域表
        $res = Area::create($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    //区域编辑页面
    public function getEdit(Request $request)
    {
        $id = $request -> input('id');
        $data = Area::find($id) -> toarray();
        return view('admin.area.edit',['data'=>$data]);
    }

    //编辑处理页面
    public function postUpdate(Request $request)
    {
        $id = $request -> input('id');
        $info = $request -> except('id','_token');
        $res = Area::find($id) -> update($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    //删除处理页面
    public function postDestroy(Request $request)
    {
        $id = $request -> input('id');
        //查询区域下是否有建筑,有不能删除
        $count = Room::where('area_id',$id) -> count();
        if($count == 0){
            //查询区域和客户的关系表并删除
            Areamember::where(['area_id'=>$id])-> delete();
            $res = Area::destroy($id);
            if($res){
                $data['status'] = 1;
            }else{
                $data['status'] = -1;
            }
        }else{
            $data['status'] = -2;
        }
        echo json_encode($data);
    }

    //追加负责人(客户)
    public function postAddmember(Request $request)
    {
        //获取post数据
        $area_id = $request -> input('id');
        $member = $request -> input('member');
        $info = $this -> addMember('area',$area_id,$member);
        echo json_encode($info);
    }

    //删除负责人(客户)
    public function postDelmember(Request $request)
    {
        //获取post数据
        $area_id = $request -> input('area_id');
        $member_id = $request -> input('member_id');
        //查询区域和客户的关系表并删除
        $info = $this -> delMember('area',$area_id,$member_id);
        echo json_encode($info);
    }
}
